package com.woniu40.petai_user.web;

import com.woniu40.petai_user.entity.Address;
import com.woniu40.petai_user.entity.Order;
import com.woniu40.petai_user.entity.OrderItem;
import com.woniu40.petai_user.entity.User;
import com.woniu40.petai_user.service.*;
import com.woniu40.petai_user.vo.GoodsVo;
import com.woniu40.petai_user.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    GoodsService goodsService;
    @Autowired
    OrderService orderService;
    @Autowired
    AddressService addressService;
    @Autowired
    OrderItemService orderItemService;
    @Autowired
    UserService userService;


    @RequestMapping("/list")
    public String list(HttpSession session, Map map){
        User user = (User) session.getAttribute("user");
        Integer userId = user.getUserId();
        List<Order> orderList = orderService.findOrderAll(userId);
        User userById = userService.findUserById(userId);

        map.put("orderlist",orderList);
        map.put("user",userById);
        List<Address> addressList = addressService.findAddressByUserId(userId);
        map.put("addressList",addressList);
        return "my-account";
    }

    //修改订单状态
    //订单状态 1 = 未付款  2 = 已付款 3 = 已发货 4 = 已收货 5 = 申请退款 6 = 退款中 7=退款申请失败
    @RequestMapping("/update")
    public String update(Integer status,String orderNum){
        Order order = orderService.findOrderByNum(orderNum);
        order.setOrderStatus(status);
        orderService.modifyOrder(order);

        return "index";
    }

    @RequestMapping("/toMyOrder")
    public String toMyOrder(Integer orderId,Map map){
        Order order = orderService.findOrderById(orderId);

        List<OrderItemVo> goodsInfo = new ArrayList<>();

        Address address = addressService.findAddressById(order.getAddressId());
        List<OrderItem> orderItemList=orderItemService.findByOrderId(orderId);
        BigDecimal total = new BigDecimal("0.00");

        for (OrderItem orderItem : orderItemList) {
            GoodsVo vo = goodsService.findVoByGoodsId(orderItem.getGoodsId());

            OrderItemVo itemVo = new OrderItemVo();
            itemVo.setGoodsId(orderItem.getGoodsId());
            itemVo.setGoodsName(vo.getGoodsName());
            itemVo.setGoodsTypeName(vo.getGoodsTypeName());
            itemVo.setBuyCount(orderItem.getBuyCount());
            itemVo.setAllPrice(orderItem.getAllPrice());
            itemVo.setCover(vo.getCover());

            total=total.add(orderItem.getAllPrice());
            goodsInfo.add(itemVo);
        }

        map.put("address",address);
        map.put("goodsInfo",goodsInfo);
        map.put("total",total);

        return "myorder";
    }

    @RequestMapping("/removeProduct")
    public void removeProduct(String orderNum){
        orderService.removeProduct(orderNum);
    }
}
